package com.example.pageexample;

import androidx.appcompat.app.AppCompatActivity;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;
import androidx.paging.PagedList;
import androidx.recyclerview.widget.DividerItemDecoration;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.StaggeredGridLayoutManager;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.example.pageexample.database.Picture;
import com.example.pageexample.database.PictureHelper;
import com.example.pageexample.view.StaggeredDecoration;
import com.example.pageexample.viewmodel.PictureViewModel;
import com.example.pageexample.view.PictureAdapter;
import com.example.pageexample.page.ThreadExecutor;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {
    private static final String TAG = "MainActivity";
    private PictureViewModel mViewModel;
    private PictureAdapter mAdapter;
    //当前数据库列表总长度
    private int mCount;
    String[] pictures = {"https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fc-ssl.duitang.com%2Fuploads%2Fitem%2F201909%2F18%2F20190918210806_8nssN.thumb.700_0.jpeg&refer=http%3A%2F%2Fc-ssl.duitang.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1620396123&t=e611b214970166ed1856fd9550a1f031",
            "https://pics3.baidu.com/feed/ca1349540923dd54d918df85ae64f4d89d824863.jpeg?token=c3be448f24d5cd73f5c8c445b042798a&s=65C89B47DBD2889C3B3835990300C012",
            "https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fpic.jj20.com%2Fup%2Fallimg%2Fmx08%2F0610191P500%2F1Z6101P500-1.jpg&refer=http%3A%2F%2Fpic.jj20.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1620396200&t=2428e0b0f6b19b48de8484af46bdad24",
            "https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fd.fengfeng.cc%2Fnews%2Fyule%2F2017-07-18%2F71989998810a5493567619892c762fa5.jpg&refer=http%3A%2F%2Fd.fengfeng.cc&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1620396200&t=3946f9a42da5ec8432ecfa07bd0e151d"};
    String[] names = {"佟1","佟2","杨幂1","杨幂2"};

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mViewModel = new ViewModelProvider(this).get(PictureViewModel.class);
        initView();
    }

    //初始化视图
    private void initView(){
        RecyclerView recyclerView = findViewById(R.id.recyclerview);
        TextView textviewDataAmount = findViewById(R.id.textview_data_amount);
        Button buttonAddData = findViewById(R.id.button_add_data);
        Button buttonShowData = findViewById(R.id.button_show_data);
        //监听数据总量
        mViewModel.getCountLiveData().observe(this, new Observer<Integer>() {
            @Override
            public void onChanged(Integer count) {
                mCount = count;
                if(count < 0){
                    textviewDataAmount.setText("本地暂无数据,请点击加载数据");
                }else{
                    textviewDataAmount.setText("本地已有"+count+"张图片");
                }
            }
        });
        mViewModel.requestTotalCount();
        //监听列表数据变化
        mViewModel.getPagedListLiveData().observe(this, new Observer<PagedList<Picture>>() {
            @Override
            public void onChanged(PagedList<Picture> pageList) {
                mAdapter.submitList(pageList);
                Log.d(TAG,"size = "+pageList.size());
            }
        });
        buttonAddData.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(mCount > 0){
                    Toast.makeText(MainActivity.this,"本地已有数据，请勿重复添加",Toast.LENGTH_SHORT).show();
                }else{
                    insertData();
                }
            }
        });
        buttonShowData.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ThreadExecutor.getInstance().execute(new Runnable() {
                    @Override
                    public void run() {
                        //执行任务
                        Picture picture = new Picture();
                        picture.setName(names[0]+System.currentTimeMillis());
                        picture.setUrl(pictures[0]);
                        PictureHelper.getInstance().insert(picture);
                    }
                });
            }
        });
        StaggeredGridLayoutManager layoutManager = new StaggeredGridLayoutManager(2,RecyclerView.VERTICAL);
        recyclerView.setLayoutManager(layoutManager);
        RecyclerView.ItemDecoration decoration = new StaggeredDecoration(5);
        recyclerView.addItemDecoration(decoration);
        mAdapter = new PictureAdapter();
        recyclerView.setAdapter(mAdapter);
    }

    //向数据库添加模拟数据
    private void insertData(){
        List<Picture> pictureList = new ArrayList<>();
        for(int i = 0 ; i < 100 ; i++){
            Picture picture = new Picture();
            picture.setName(names[i%names.length]+"-"+i);
            picture.setUrl(pictures[i%names.length]);
            pictureList.add(picture);
        }
        PictureHelper helper = PictureHelper.getInstance();
        helper.insertAll(pictureList);
    }
}